/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.grandmaster.service.impl;

import co.yixiang.api.ApiResult;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.grandmaster.domain.YxGrandmaster;
import co.yixiang.modules.grandmaster.service.YxGrandmasterService;
import co.yixiang.modules.grandmaster.service.dto.YxGrandmasterDto;
import co.yixiang.modules.grandmaster.service.dto.YxGrandmasterPage;
import co.yixiang.modules.grandmaster.service.dto.YxGrandmasterQueryCriteria;
import co.yixiang.modules.grandmaster.service.mapper.YxGrandmasterMapper;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2021-08-12
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxGrandmaster")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxGrandmasterServiceImpl extends BaseServiceImpl<YxGrandmasterMapper, YxGrandmaster> implements YxGrandmasterService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxGrandmasterQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxGrandmaster> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxGrandmasterDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxGrandmaster> queryAll(YxGrandmasterQueryCriteria criteria){
        /*return baseMapper.selectList(QueryHelpPlus.getPredicate(YxGrandmaster.class, criteria));*/
        return baseMapper.queryAll(criteria);
    }


    @Override
    public void download(List<YxGrandmasterDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxGrandmasterDto yxGrandmaster : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("用户昵称", yxGrandmaster.getNickname());
            map.put("用户头像", yxGrandmaster.getAvatar());
            map.put("荣誉", yxGrandmaster.getHonor());
            map.put("简介", yxGrandmaster.getIntroduction());
            map.put("创建时间", yxGrandmaster.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public ApiResult<Page<YxGrandmasterPage>> getGrandmasterPage(Integer page, Integer limit) {
        Page page1=new Page(page,limit);
        Page<YxGrandmasterPage> paList = this.baseMapper.getGrandmasterPage(page1);
        return ApiResult.ok(paList);
    }


    @Override
    public ApiResult<Object> getGrandmasterDetails(Integer id) {
        YxGrandmaster yxGrandmaster = this.baseMapper.selectById(id);
        return ApiResult.ok(yxGrandmaster);
    }
}
